/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <QRegExp>
#include "log.h"
#include "components/chan.h"
#include "components/board.h"
#include "network/httprequest.h"
#include "network/httpconnector.h"
#include "tasks/tasksendpost.h"
#include "kusabaagent.h"

namespace Wishmaster
{
	class KusabaTaskSendPost : public TaskSendPost
	{
		Q_OBJECT
	public:
		KusabaTaskSendPost(const PostDescriptor& post, const QString& captcha, Connector* connector, const QString& msg, KusabaAgent* agent) :
			TaskSendPost(post, captcha, connector, msg),
			m_post(post),
			m_captcha(captcha),
			m_connector(connector),
			m_agent(agent),
			m_error(false)
		{
		}

		virtual bool isError() const
		{
			return m_error;
		}

		virtual QString errorMessage() const
		{
			return m_errorMsg;
		}

	protected slots:
		virtual void slot_result(const RequestPtr& request, const QByteArray& arr)
		{
			log(llDebug, "KusabaTaskSendPost::slot_result");
			// Check for errors
			QString regexp = QString::fromUtf8("<h2 style=\"font-size: 2em;font-weight: bold;text-align: center;\">([^<>]*)</h2>");
			QRegExp rx(regexp);
			if(rx.indexIn(QString::fromUtf8(arr)) != -1)
			{
				log(llError, "Error: %s", qPrintable(rx.cap(1)));
				m_error = true;
				m_errorMsg = rx.cap(1);
			}
			TaskSendPost::slot_result(request, arr);
		}

	protected:
		virtual RequestPtr makeRequest(const PostDescriptor& post, const QString& captcha, Connector* connector)
		{
			HttpConnector* httpConnector = qobject_cast<HttpConnector*>(connector);
			if(!httpConnector)
				return RequestPtr();
			log(llDebug, "KusabaTaskSendPost::makeRequest");
			QString plainboardname = post.board;
			plainboardname.remove('/');
			QString referer;
			if(post.parent_thread.threadNumericId() != 0)
				referer = QString("http://%1%2res/%3.html").arg(post.parent_thread.chanName()).
					arg(post.parent_thread.boardId()).
					arg(post.parent_thread.threadNumericId());
			else
				referer = QString("http://%1%2").arg(post.parent_thread.chanName()).
					arg(post.parent_thread.boardId());
				

			HttpRequestPtr request = httpConnector->makeHttpRequest(post.parent_thread.chanName(), QString("/board.php?dir=%1").arg(plainboardname), "POST");
			request->setReferer(referer);

			request->addPostField("board", plainboardname);
			request->addPostField("replythread", post.parent_thread.threadStringId());
			request->addPostField("MAX_FILE_SIZE", "1572864");
			request->addPostField("email", "");
			if(post.sage)
				request->addPostField("em", "sage");
			request->addPostField("subject", post.topic);
			request->addPostField("message", post.text);
			m_agent->addAttachments(request, post.attachments.count() > 0 ? post.attachments.at(0) : AttachmentDescriptor(), "imagefile");
			request->addPostField("redirecttothread", "1");
			request->addPostField("postpassword", post.password);
			request->addPostField("captcha", captcha);

			return request;
		}

	private:
		PostDescriptor m_post;
		QString m_captcha;
		Connector* m_connector;
		KusabaAgent* m_agent;
		bool m_error;
		QString m_errorMsg;
	};
}

